package com.pzhu.dormitory.controller;


import com.pzhu.dormitory.entity.PermissionEntity;
import com.pzhu.dormitory.service.PermissionService;
import com.pzhu.dormitory.utils.PageUtils;
import com.pzhu.dormitory.utils.R;
import com.pzhu.dormitory.utils.Rb;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 后台用户权限表
 *
 * @author
 * @email
 * @date 2021-04-10 08:46:25
 */
@Slf4j
@RestController
@Api(value = "PermissionController", tags = "后台用户权限表")
@RequestMapping("elective/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取后台用户权限表list")
    public R<PermissionEntity> list(@RequestParam Map<String, Object> params) {
        PageUtils<PermissionEntity> page = permissionService.queryPage(params);
        return Rb.a(PermissionEntity.class).success().page(page).build();
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取一个后台用户权限表信息")
    public R<PermissionEntity> info(@Validated @PathVariable("id") Long id) {
        PermissionEntity permission = permissionService.getById(id);
        return Rb.a(PermissionEntity.class).success().withData(permission).build();
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存后台用户权限表信息")
    @PostMapping("/save")
    public R<PermissionEntity> save(@Validated @RequestBody PermissionEntity permission) {
        permissionService.save(permission);
        return Rb.a(PermissionEntity.class).success().build();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新后台用户权限表信息")
    @PutMapping("/update")
    public R<PermissionEntity> update(@RequestBody PermissionEntity permission) {
        permissionService.updateById(permission);
        return Rb.a(PermissionEntity.class).success().build();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除后台用户权限表信息")
    @DeleteMapping("/delete")
    public R<PermissionEntity> delete(@RequestBody Long[] ids) {
        permissionService.removeByIds(Arrays.asList(ids));
        return Rb.a(PermissionEntity.class).success().build();
    }

}
